package com.comdosoft.comdoandroidbase.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.comdosoft.comdoandroidbase.R;
import com.comdosoft.comdoandroidbase.util.DataUtil;

import java.util.HashMap;
import java.util.List;

/**
 * Created by yewei on 2015/9/16.
 */
public class LeftMenuAdapter extends BaseAdapter {

    private Context mContext;
    private LayoutInflater mInflater;
    private List<HashMap<String, Object>> menulist;
    private int selectedindex = 0;

    public LeftMenuAdapter(Context context, List<HashMap<String, Object>> list) {
        this.mContext = context;
        this.mInflater = LayoutInflater.from(context);
        this.menulist = list;
    }

    @Override
    public int getCount() {
        if (menulist == null)
            return 0;
        return menulist.size();
    }

    @Override
    public Object getItem(int position) {
        if (menulist == null || position > menulist.size() - 1)
            return null;
        return menulist.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewholder = null;

        if (convertView == null) {
            viewholder = new ViewHolder();
            convertView = mInflater.inflate(R.layout.leftmenufragment_item, parent, false);
            viewholder.tvName = (TextView) convertView.findViewById(R.id.tv_name);
            convertView.setTag(viewholder);
        } else
            viewholder = (ViewHolder) convertView.getTag();

        viewholder.tvName.setText(menulist.get(position).get(DataUtil.MENU_KEY_ITEM_NAME).toString());

        if (position == selectedindex)
            viewholder.tvName.setBackgroundResource(R.drawable.leftmenufragment_item_selected_bg);
        else
            viewholder.tvName.setBackgroundResource(R.drawable.leftmenufragment_item_bg);


        return convertView;
    }


    static class ViewHolder {

        TextView tvName;
    }


    public void setSelectedIndex(int index) {

        if (menulist != null && index < menulist.size()) {
            selectedindex = index;
            notifyDataSetChanged();
        }
    }
}
